/* Example code for driving a single 7-segment display with
 a 74HC595 8-bit shift register
 
 The code cycles through the the entire 7-segment character
 set that was written by Eli McIlveen:
 
 (http://www.forgeryleague.com/lab/entry/arduino_7_segment_output/)
 
 In case of a COMMON ANODE display the bytes need to be inverted
 as shown in the loop() function: bitsToSend = bitsToSend ^ B11111111
 
 Hardware:
 
 * 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
   as detailed below.
 
 * Pins to connect to common-cathode LED display via a 74HC595:
   DP-15, A-1, B-2, C-3, D-4, E-5, F-6, G-7 (shiftOut using LSBFIRST)
   Or:
   DP-7, A-6, B-5, C-4, D-3, E-2, F-1, G-15 (shiftOut using MSBFIRST)
 
 Created 12 October 2010
 by Ole Weidner (http://www.oleweidner.com/?p=434)
 */


// Yellow pin 11
// Green pin 12
// Blue pin 8

// Potent A5
// Button 5
// Base Pin 2
const byte ledCharSet[128] =
{
  	// 00-0F: Hex digits
  	B01111110, B00110000, B01101101, B01111001,	// 0123
  	B00110011, B01011011, B01011111, B01110000,	// 4567
  	B01111111, B01110011, B01110111, B00011111,	// 89AB
  	B01001110, B00111101, B01001111, B01000111,	// CDEF
 
  	// 10-1F: Figure-8 drawing (8-character cycle)
  	B01000000, B00100000, B00000001, B00000100,	// 1-segment
  	B00001000, B00010000, B00000001, B00000010,
 
  	B01100000, B00100001, B00000101, B00001100,	// 2-segment
  	B00011000, B00010001, B00000011, B01000010,
 
  	// 20-2F: Punctuation (barely recognizable!)
    	B00000000, B10100000, B00100010, B00111111,	//  !"#
    	B01011010, B01001001, B00000111, B00000010,	// $%&amp;'
    	B01001110, B01111000, B01100011, B00110001,	// ()*+
    	B00010000, B00000001, B10000000, B00100101,	// ,-./
 
    	// 30-3F: Decimal digits (alternate) and more punctuation
    	B01111110, B00110000, B01101101, B01111001,	// 0123
    	B00110011, B01011011, B00011111, B01110010,	// 4567
    	B01111111, B01110011, B01001000, B01010000,	// 89:;
    	B00001101, B00001001, B00011001, B11100000,	// &lt;=&gt;?
 
    	// 40-5F: Capital letters and punctuation
    	B01101110, B01110111, B00011111, B01001110,	// @ABC
    	B01111100, B01001111, B01000111, B01011110,	// DEFG
    	B00110111, B00000110, B00111100, B01010111,	// HIJK
    	B00001110, B01110110, B00010101, B01111110,	// LMNO
 
    	B01100111, B01110011, B01000110, B01011011,	// PQRS
    	B01110000, B00111110, B00111110, B00101011,	// TUVW
    	B01110101, B00111011, B01101101, B01001110,	// XYZ[
    	B00010011, B01111000, B01100010, B00001000,	// \]^_
 
    	// 60-7F: Lowercase letters and punctuation
    	B01100000, B01111101, B00011111, B00001101,	// `abc
    	B00111101, B01101111, B01000111, B01111011,	// defg
    	B00010111, B00010000, B00011000, B00101111,	// hijk
    	B00001100, B01010101, B01101010, B00011101,	// lmno
 
    	B01100111, B01110011, B00000101, B00010011,	// pqrs
    	B00001111, B00011100, B00100011, B01011101,	// tuvw
    	B01101100, B00111011, B00100101, B01000011,	// xyz{
    	B00110110, B01100001, B01000000, B11111111	// |}~
};


const int latchPin = 8; //Pin connected to latch pin (ST_CP) of 74HC595
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
const int buttonPin = 5;
const int basePin = 2;

int potentPin = A5;
// 0 None
// 1 Fill
// 2 Weigh
int state;

byte bitsToSendFirst;
byte bitsToSendSecond;

boolean buttonPushed = false;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(potentPin, INPUT);
  pinMode(13, OUTPUT);
  pinMode(buttonPin, OUTPUT);
  pinMode(basePin, OUTPUT);
  state = 0;
  Serial.begin(9600);
  //Serial.println("reset");
  
  bitsToSendFirst = B00000000;
  bitsToSendSecond = B00000000;
}

void loop() {
  
  if( Serial.available() > 0 ) {
    char res = Serial.read();
    
    if( res == 'D') state = 0;
    else if( res == 'F') state = 1;
    else if( res == 'W') state = 2;
    else if( res == 'G') state = 3;
    else {
      state = 4;
      
     int received = ((int) res) + 1;
     int firstDigit = (int) (received / 10);
     int secondDigit = (int) (received % 10);
     bitsToSendSecond = ledCharSet[received];
      
    if( firstDigit == 1 ) bitsToSendFirst = ledCharSet[1];
    }
  }
  
  if( state == 0 ) {
    bitsToSendFirst = B00000000;
    bitsToSendSecond = B00000000;
  }
  if( state == 1 ) {
    bitsToSendFirst = B00000000;
    bitsToSendSecond = B00000000;
    
    int data = analogRead(potentPin);
    //Serial.println(data);
    int mapped = map(data, 0, 1023, 1, 10);
    
    int firstDigit = (int) (mapped / 10);
    int secondDigit = (int) (mapped % 10);
    
    bitsToSendSecond = ledCharSet[secondDigit];
    
    if( firstDigit == 1 ) bitsToSendFirst = ledCharSet[1];
  }
  if( state == 2 ) {
  }
  if( state == 3 ) {
    int data = analogRead(potentPin);
    //Serial.println(data);
    int mapped = map(data, 0, 1023, 1, 10);
    
    Serial.write( (mapped - 1) );
    state = 0;
    
    
    // Reset the display
    digitalWrite(latchPin, LOW);
 
    // shift the bits out:
  
    shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
    shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
  
 
  // turn on the output so the LEDs can light up:
    digitalWrite(latchPin, HIGH);
    
    digitalWrite(basePin, HIGH);
    delay(mapped * 1000);
    digitalWrite(basePin, LOW);
  } 
  
  else if( state == 4 ) {
    
  }
  
  
  byte bitsToSendFirstFinal = bitsToSendFirst ^ B11111111;
  byte bitsToSendSecondFinal = bitsToSendSecond ^ B11111111; 
  
  digitalWrite(latchPin, LOW);
 
    // shift the bits out:
  
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSendFirstFinal);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSendSecondFinal);
  
 
  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);
  
  
  int buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(13, HIGH);  
    if( !buttonPushed ) {
      if( state != 1 ) {
        state = 1;
        Serial.write('F');
      }
      else {
        state = 3;
        Serial.write('G');
      }
      buttonPushed = true;
    }
    
  } 
  else {
    // turn LED off:
    digitalWrite(13, LOW); 
    buttonPushed = false;
  }
}
